package com.demo.cms.action.manage;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.i18n.LocaleContextHolder;

import com.demo.Constants;
import com.demo.cms.model.Category;
import com.demo.cms.model.Channel;
import com.demo.cms.service.CategoryService;
import com.demo.cms.service.ChannelService;
import com.demo.core.dao.base.support.ResultSearchResult;
import com.demo.core.dao.base.support.SearchArgumentSupport;
import com.demo.core.model.Config;
import com.demo.core.model.Site;
import com.demo.core.model.Template;
import com.demo.core.model.support.AdjacencyListModel;
import com.demo.core.service.base.ConfigService;
import com.demo.core.service.base.TemplateService;
import com.demo.core.util.collection.JsonUtils;
import com.demo.security.annotation.function.Function;
import com.demo.webapp.action.PageAction;
import com.demo.webapp.action.TemplateProvider;
import com.demo.webapp.context.MultisiteContextHolder;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;

/**
 * 栏目相关Action
 * 
 * @author ryuu.kk
 *
 */
@Function(id = 104100, pid = 10, code = "manage.category")
public class CategoryAction extends PageAction implements TemplateProvider {

	/**
	 * 版本序列化ID
	 */
	private static final long serialVersionUID = 5001171674831180755L;

	/**
	 * 模板模型
	 */
	private Template template;
	
	/**
	 * 栏目树
	 */
	private AdjacencyListModel tree;
	
	/**
	 * 频道ID
	 */
	private Long channelId;
	
	/**
	 * 栏目ID
	 */
	private Long id;
	
	/**
	 * 父级栏目
	 */
	private Long pid;
	
	/**
	 * 栏目模型
	 */
	private Category category;
	
	/**
	 * 栏目列表
	 */
	private List<Category> categoryList;
	
	/**
	 * 栏目模板
	 */
	private List<Template> templateList;
	
	/**
	 * 频道列表
	 */
	private List<Channel> channelList;
	
	/**
	 * 栏目访问服务
	 */
	private CategoryService categoryService;
	
	/**
	 * 频道服务
	 */
	private ChannelService channelService;
	
	/**
	 * 模板服务
	 */
	private TemplateService templateService;
	
	/**
	 * 配置服务
	 */
	private ConfigService configService;
	
	/**
	 * 国际化配置
	 */
	private Config configI18N;
	
	/**
	 * 栏目树结构的json字符串
	 */
	private String json;
	
	/**
	 * 初始页
	 * @param resultMap
	 */
	@Function(id = 104101, pid = 104100, code = "manage.category.view")
	public String execute() {
		//统计
		return SUCCESS;
	}
	
	/**
	 * 频道访问服务
	 */
	//private ChannelService channelService;
	/**
	 * 取得文章列表
	 * 通过栏目ID查询文章列表
	 * @return resultMap
	 */
	@Function(id = 104110, pid = 104100, code = "manage.category.list")
	public String list() {
		// 取得站点ID
		Site site = MultisiteContextHolder.getSite();
		Long siteId = site.getId();
				
		//取得语言
		String i18n = LocaleContextHolder.getLocale().toString();
		if (log.isDebugEnabled()) {
			log.debug("Current-Language is " + i18n);
		}
		// 取得频道列表
		this.channelList = channelService.findList(siteId);
		
		// 取得语言配置
		this.configI18N = configService.query(siteId, Constants.CONFIG_I18N);

		// 设置默认频道ID
		if (channelId == null && channelList.size() > 0) {
			for (Channel c : channelList) {
				if (Channel.STATUS_DEFAULT.equals(c.getStatus())) {
					channelId = c.getId();
					break;
				}
			}
			// 未设置默认频道,则使用第一个频道作为默认
			if (channelId == null) {
				channelId = channelList.get(0).getId();
			}
		}
		if (id == null) {
			Category category = categoryService.findRoot(channelId);
			if (category == null) {
				addActionMessage("当前频道不存在栏目,请先添加");
				return SUCCESS;
			}
			id = category.getId();
		}
		
		SearchArgumentSupport args = getArgumentSupport();
		ResultSearchResult<Category> result = categoryService.search(channelId, id, args);
		this.setResultSupport(result);
		categoryList = new ArrayList<Category>(result.getResults());
		return SUCCESS;
	}
	
	/**
	 * 创建栏目
	 * @return resultMap
	 */
	@Function(id = 104120, pid = 104100, code = "manage.category.create")
	public String create() {
		
		// 取得站点ID
		Site site = MultisiteContextHolder.getSite();
		Long siteId = site.getId();
			
		// 取得语言配置
		this.configI18N = configService.query(siteId, Constants.CONFIG_I18N);
		// 取得文章单页模板
		this.templateList = getTemplate();
		// 取得频道列表
		this.channelList = channelService.findList(siteId);
		
		categoryList = categoryService.findList(channelId);
		
		return SUCCESS;
	}

	/**
	 * 栏目保存
	 * @return resultMap
	 */
	@Function(id = 104130, pid = 104100, code = "manage.category.save")
	@InputConfig(methodName="create")
	public String save() {
		
		if (category.getPid() == null) {
			Category root = categoryService.findRoot();
			if (root == null) {
				addActionError("请联系管理员新建栏目根节点");
				return CALLBACK;
			}
			category.setPid(root.getId());
		}
		
		//保存栏目
		categoryService.save(category);
		addActionMessage(getText("category.added", "栏目已创建.", category.getTitle()));
		return CALLBACK;
	}
	
	/**
	 * 栏目编辑页面
	 * @return resultMap
	 */
	@Function(id = 104140, pid = 104100, code = "manage.category.edit")
	public String edit() {
		
		// 取得站点ID
		Site site = MultisiteContextHolder.getSite();
		Long siteId = site.getId();
		// 取得语言配置
		this.configI18N = configService.query(siteId, Constants.CONFIG_I18N);
		// 包含文章内容,使用query方法
		this.category = categoryService.find(id);
		// 取得文章单页模板
		this.templateList = getTemplate();
		// 取得频道列表
		this.channelList = channelService.findList(siteId, category.getI18n());
		// 栏目的父子关系
		this.categoryList = categoryService.findList(channelId, category.getI18n());
		// 频道列表
		return SUCCESS;
	}
	
	/**
	 * 栏目更新
	 * @return resultMap
	 */
	@Function(id = 104150, pid = 104100, code = "manage.category.update")
	public String update() {
		
		categoryService.update(id, category, true);
		
		addActionMessage(getText("category.updated", "栏目信息更新成功."));
		return CALLBACK;
	}
	
	/**
	 * 删除栏目
	 * @return 删除栏目
	 */
	@Function(id = 104160, pid = 104100, code = "manage.category.delete")
	public String delete() {
		
		categoryService.delete(id);
		
		addActionMessage(getText("category.deleted", "栏目信息删除成功."));
		return CALLBACK;
	}
	
	/**
	 * 预览频道信息
	 * @return String
	 */
	@Function(id = 104170, pid = 104100, code = "manage.category.review")
	public String review() {
		
		// 取得渲染模板
		template = templateService.find(category.getTemplateId());
				
		// 使用自定义渲染
		return "selfRender";
	}
	
	/**
	 * 查询所有栏目
	 * @return resultMap
	 */
	@Function(id = 104180, pid = 104100, code = "manage.category.tree")
	public String tree() {
		// 取得站点ID
		Site site = MultisiteContextHolder.getSite();
		Long siteId = site.getId();
		// 取得频道列表
		this.channelList = channelService.findList(siteId);
		// 设置默认频道ID
		if (channelId == null && channelList.size() > 0) {
			channelId = channelList.get(0).getId();
		}
		tree = categoryService.queryRootTree(channelId);
		// 生成json树
		json = JsonUtils.zTree(tree, true);
		return SUCCESS;
	}
	
	/**
	 * 取得栏目模板
	 * @return List<Template>
	 */
	private List<Template> getTemplate() {
		Site site = MultisiteContextHolder.getSite();
		if (site == null) {
			return null;
		}
		Long siteId = site.getId();
		// 取得文章模板列表, 不包含模板内容
		return templateService.findList(siteId, Constants.TYPE_CATEGORY, false);
	}
	
	// ~~ settings here -------------------------------------------------------------------------
	public Long getChannelId() {
		return channelId;
	}

	public void setChannelId(Long channelId) {
		this.channelId = channelId;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getPid() {
		return pid;
	}

	public void setPid(Long pid) {
		this.pid = pid;
	}

	public List<Category> getCategoryList() {
		return categoryList;
	}

	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

	public AdjacencyListModel getTree() {
		return tree;
	}

	public String getJson() {
		return json;
	}

	public List<Template> getTemplateList() {
		return templateList;
	}

	public List<Channel> getChannelList() {
		return channelList;
	}

	public Config getConfigI18N() {
		return configI18N;
	}

	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public void setTemplateService(TemplateService templateService) {
		this.templateService = templateService;
	}

	public void setChannelService(ChannelService channelService) {
		this.channelService = channelService;
	}

	public void setConfigService(ConfigService configService) {
		this.configService = configService;
	}

	@Override
	public Template getTemplateModel() {
		return this.template;
	}
}
